package com.project.lk.nurseapp.nurse.doctorapply.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;
import com.project.lk.nurseapp.utils.UserInfoUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.FormBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class ApplyModel extends PostBaseModel {

    String payWay="";
    public void postLoadJson(String url,String baseDoctorInfoId,String userConponId,String sessionId,String payMentWay){
        payWay=payMentWay;
        Random random = new Random();
        int nextInt = random.nextInt();
        FormBody.Builder builder = new FormBody.Builder();
        long currentTimeMillis = System.currentTimeMillis();
        builder.add("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                .add("timestamp",currentTimeMillis+"")
                .add("nonce",nextInt+"")
                .add("accuntId",new UserInfoUtils().getAccountId())
                .add("baseDoctorInfoId",baseDoctorInfoId)
                .add("baseUserInfoId",new UserInfoUtils().getBaseUserInfoId())
                .add("userConponId",userConponId)
                .add("sessionId",sessionId)
                .add("payMentWay",payMentWay);
        getJsonData(url,builder.build());
    }
    @Override
    public List analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);
        try{
            JSONObject jsonObject=new JSONObject(json);
            String msg = jsonObject.getString("msg");
            String info = jsonObject.getString("info");
            ToastUtils.showShortToast(info);//显示提示信息
            if (TextUtils.equals(msg,"success")){
                JSONObject data = jsonObject.getJSONObject("data");
                if (data!=null){  //不等于null，采用微信或支付宝支付
                    if (TextUtils.equals(payWay,"1")) { // 微信
                        List<ApplyBean> list=new ArrayList<>();
                        ApplyBean bean = (ApplyBean) JsonUtils.getJsonUtils()
                                .toObject(new TypeToken<ApplyBean>(){}.getType(),json);
                        list.add(bean);
                        return list;
                    }else {  //支付宝
                        List<ApplyBean> list=new ArrayList<>();
                        ApplyBean bean = (ApplyBean) JsonUtils.getJsonUtils()
                                .toObject(new TypeToken<ApplyBean>(){}.getType(),json);
                        list.add(bean);
                        return list;
                    }
                }else {
                    ticketApplySuccess();
                    return null;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            getOrderFail();
        }
        return null;
    }

    List<ApplyBean> mList=new ArrayList<>();
    public List<ApplyBean> getPayList() {
        return mList;
    }

    @Override
    public void postSuccessOnMainThread(List list, String url) {
        if (TextUtils.equals(payWay,"1")) { // 微信
            mList.clear();
            mList.addAll(list);
            getOrderSuccess();
        }else {  //支付宝
            if (list!=null){
                mList.clear();
                mList.addAll(list);
                getOrderSuccess();
            }
        }
    }

    private void getOrderSuccess() {
        bridge.bridge(BaseApi.order_apply_success,null);
    }

    private void getOrderFail() {
        bridge.bridge(BaseApi.order_apply_fail,null);
    }

    private void ticketApplySuccess(){
        bridge.bridge(BaseApi.apply_ticket_success,null);
    }

    @Override
    public void postFailedOnMainThread(String url) {
        getOrderFail();
    }
}
